package com.jdbc;

import java.sql.*;

public class Text_jdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置信息
        String url="jdbc:mysql://localhost:3306/jdbc?useSSL=true&useUnicode=true&characterEncoding=UTF-8";
        String uname="root";
        String upwd="haohai";

        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库,代表数据库
        Connection connection = DriverManager.getConnection(url, uname, upwd);
        //3.向数据库发送sql对象，Statement，做增删改查(使用PreparedStatement，安全)
        Statement statement = connection.createStatement();
        //编写sql
        String sql="select * from users";
//      String sql="delete from users where id=4";
        //4.执行sql,返回一个结果集
        ResultSet resultSet = statement.executeQuery(sql);
        //返回受影响的行数
//      int i = statement.executeUpdate(sql);
        while (resultSet.next())
        {
            System.out.println("id="+resultSet.getObject("id"));
            System.out.println("uname="+resultSet.getObject("uname"));
            System.out.println("upwd="+resultSet.getObject("upwd"));
            System.out.println("email="+resultSet.getObject("email"));
            System.out.println("birthday="+resultSet.getObject("birthday"));
        }
        //6.关闭连接释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
